﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbPictureDal
	{
		public DbPictureDal()
		{
		}
 		#region Insert
 		public static int Insert(DbPicture dbPicture)
		{	
			string sql = @"Insert Into DbPicture (
					PTitle,
					PDefaultImg,
					PNotes,
					PStatus,
					PAddTime,
					POrder,
					PViews
				) values('{0}','{1}','{2}',{3},'{4}',{5},{6})";
			sql = string.Format(sql,
					SqlHelper.String(dbPicture.PTitle),
					SqlHelper.String(dbPicture.PDefaultImg),
					dbPicture.PNotes,
					dbPicture.PStatus,
					dbPicture.PAddTime,
					dbPicture.POrder,
					dbPicture.PViews
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbPicture dbPicture)
		{	
			string sql = @"Update DbPicture set 

					PTitle= '{0}',
					PDefaultImg= '{1}',
					PNotes= '{2}',
					PStatus= {3},
					PAddTime= '{4}',
					POrder= {5},
					PViews= {6}
				where

					PId= {7}";
			sql = string.Format(sql,
					SqlHelper.String(dbPicture.PTitle),
					SqlHelper.String(dbPicture.PDefaultImg),
					dbPicture.PNotes,
					dbPicture.PStatus,
					dbPicture.PAddTime,
					dbPicture.POrder,
					dbPicture.PViews,
					dbPicture.PId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 pId)
		{
		
			string sql = @"Delete from DbPicture where 
					PId= {0}";
			sql = string.Format(sql,
					pId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbPicture a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbPicture Select(Int32 pId)
		{
			DbPicture dbPicture = null;
			string sql = @"select a.* from DbPicture a  where
					a.PId= {0}
";
			sql = string.Format(sql,
					pId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbPicture = new DbPicture(ds.Tables[0].Rows[0]);					
			return dbPicture;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbPicture a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbPicture a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbPicture a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
